package leecode;

import org.junit.Test;

class Solution {
    public static boolean isHappy(int n) {
        int w = 0,sum = 0;
        boolean flag = false;
        while(sum != 1 || n > 0) {
            w = n%10;//取尾
            n /= 10;//砍尾
            sum += w * w;//累计平方和
            if(n == 0){
                if(sum == 1)
                    return true;
                if (flag && sum < 100)
                    break;
                if (!flag && sum > 100)//大于100后如果再小于100则判断为失败
                    flag = true;
                n = sum;//替换
                sum = 0;//归零
            }
        }
        return sum == 10;
    }

    public static void main(String[] args) {
        boolean b = isHappy(188);
        System.out.println(b);
    }
}

